package com.EAsystem.service.Impl;

import com.EAsystem.aop.AutoFillTime;
import com.EAsystem.entity.Announcement;
import com.EAsystem.mapper.AnnouncementMapper;
import com.EAsystem.service.AnnouncementService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public List<Announcement> findByTypeOne() {
        return announcementMapper.findByTypeOne();
    }

    @Override
    public List<Announcement> findByTypeTwo() {
        return announcementMapper.findByTypeTwo();
    }

    @Override
    public List<Announcement> findByTypeThree() {
        return announcementMapper.findByTypeThree();
    }

    @Override
    public PageInfo<Announcement> findByTypeOneWithPage(int pageNum, int pageSize) {
        try {
            // 使用PageHelper设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            // 调用DAO层的分页查询方法
            Page<Announcement> page = announcementMapper.findByTypeOneWithPage();
            // 将分页结果封装为PageInfo对象
            return new PageInfo<>(page);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageInfo<>();
        }
    }

    @Override
    public PageInfo<Announcement> findByTypeTwoWithPage(int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            Page<Announcement> page = announcementMapper.findByTypeTwoWithPage();
            return new PageInfo<>(page);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageInfo<>();
        }
    }

    @Override
    public PageInfo<Announcement> findByTypeThreeWithPage(int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            Page<Announcement> page = announcementMapper.findByTypeThreeWithPage();
            return new PageInfo<>(page);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageInfo<>();
        }
    }

    @Override
    public PageInfo<Announcement> findByConditionPage(Announcement announcement, int pageNum, int pageSize) {
        // 使用PageHelper设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 调用DAO层的条件分页查询方法
        Page<Announcement> page = announcementMapper.findByConditionWithPage(announcement);
        // 将分页结果封装为PageInfo对象
        return new PageInfo<>(page);
    }





    @Override
    @AutoFillTime(createTime = true, updateTime = true)
    public void save(Announcement announcement) {
        announcementMapper.save(announcement);
    }

    @Override
    public void delete(Integer id) {
        announcementMapper.delete(id);
    }

    @Override
    public void edit(Integer id, String title, String content, Integer type, String updateTime) {
        announcementMapper.edit(id, title, content, type, updateTime);
    }

    @Override
    public Announcement findById(Integer id) {
        return announcementMapper.findById(id);
    }
}
